/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.coupon.entity.CouponItem
   Module Description   :

   Date Created      : 2009/11/24
   Original Author   : jeff.ma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.coupon.entity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import com.yaodian100.core.common.entity.BaseEntity;
import com.yaodian100.ec.order.entity.OrderProduct;

/**
 * @author jeff.ma
 * 
 */
@javax.persistence.Entity
@javax.persistence.Table(name = "EC_COUPON_ITEM")
@NamedQueries( {
		@NamedQuery(name = "CouponItem.cleanByCoupon", query = "delete from CouponItem c where c.coupon.uuid=:couponUuid"),
		@NamedQuery(name = "CouponItem.getFreeItems", query = "from CouponItem c where c.memberId is null and (c.startDate <= :nowDate and :nowDate <= c.endDate and c.coupon.status=:couponStatus)"),
		@NamedQuery(name = "CouponItem.getMemberUsedItems", query = "from CouponItem c where c.orderId is not null and c.memberId=:memberId and c.useDate >= :limitedDate order by c.useDate"),
		@NamedQuery(name = "CouponItem.getMemberFreeItemsTotal", query = "select sum(c.coupon.faceValue) from CouponItem c where c.memberId=:memberId and c.orderId is null and (c.startDate <= :nowDate and :nowDate <= c.endDate and c.coupon.status<>:couponStatus)"),
		@NamedQuery(name = "CouponItem.getMemberFreeItems", query = "from CouponItem c where c.memberId=:memberId and c.orderId is null and (c.startDate <= :nowDate and :nowDate <= c.endDate and c.coupon.status<>:couponStatus) order by c.endDate asc"),
		@NamedQuery(name = "CouponItem.getMemberListItems", query = "from CouponItem c where c.memberId=:memberId and c.orderId is null and (c.startDate <= :nowDate and :endDate <= c.endDate and c.coupon.status<>:couponStatus) order by c.endDate asc"),
		@NamedQuery(name = "CouponItem.getMemberFreeWaitItems", query = "from CouponItem c where c.memberId=:memberId and c.orderId is null and (c.startDate > :nowDate and c.coupon.status<>:couponStatus)  order by c.startDate asc") })
public class CouponItem extends BaseEntity {

	/** serialVersionUID */
	private static final long serialVersionUID = -3177021841117649254L;
	@Id
	@GeneratedValue(generator = "gen_uuid")
	@GenericGenerator(name = "gen_uuid", strategy = "uuid")
	@Column(name = "ITEM_UUID")
	private String uuid;
	@ManyToOne(targetEntity = Coupon.class)
	@JoinColumn(name = "COUPON_UUID", nullable = false)
	@LazyToOne(LazyToOneOption.FALSE)
	private Coupon coupon;
	@Column(name = "COUPON_CODE", length = 10)
	private String code;
	@Column(name = "MEMBER_ID", length = 10)
	private String memberId;
	@Column(name = "ORDER_ID", length = 20)
	private String orderId;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "REG_DATE")
	private Date regDate;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "USE_DATE")
	private Date useDate;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "START_DATE")
	private Date startDate;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "END_DATE")
	private Date endDate;

	@Transient
	private List<OrderProduct> products;

	/** default constructor */
	public CouponItem() {
		super();
	}

	/** default constructor */
	public CouponItem(Coupon coupon) {
		super();
		this.coupon = coupon;
	}

	/** default constructor */
	public CouponItem(Coupon coupon, String memberId, Date regDate) {
		super();
		this.coupon = coupon;
		this.memberId = memberId;
		this.regDate = regDate;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @return the coupon
	 */
	public Coupon getCoupon() {
		return coupon;
	}

	/**
	 * @return the memberId
	 */
	public String getMemberId() {
		return memberId;
	}

	/**
	 * @return the orderId
	 */
	public String getOrderId() {
		return orderId;
	}

	/**
	 * @return the products
	 */
	public List<OrderProduct> getProducts() {
		if (products == null) {
			products = new ArrayList();
		}
		return products;
	}

	/**
	 * @return the regDate
	 */
	public Date getRegDate() {
		return regDate;
	}

	/**
	 * @return the useDate
	 */
	public Date getUseDate() {
		return useDate;
	}

	/**
	 * @return the uuid
	 */
	public String getUuid() {
		return uuid;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @param coupon the coupon to set
	 */
	public void setCoupon(Coupon coupon) {
		this.coupon = coupon;
	}

	/**
	 * @param memberId the memberId to set
	 */
	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	/**
	 * @param products the products to set
	 */
	public void setProducts(List<OrderProduct> products) {
		this.products = products;
	}

	/**
	 * @param regDate the regDate to set
	 */
	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}

	/**
	 * @param useDate the useDate to set
	 */
	public void setUseDate(Date useDate) {
		this.useDate = useDate;
	}

	/**
	 * @param uuid the uuid to set
	 */
	private void setUuid(String uuid) {
		this.uuid = uuid;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("CouponItem [uuid=");
		builder.append(uuid);
		builder.append(", code=");
		builder.append(code);
		builder.append(", coupon=");
		builder.append(coupon);
		builder.append(", memberId=");
		builder.append(memberId);
		builder.append(", orderId=");
		builder.append(orderId);
		builder.append(", regDate=");
		builder.append(regDate);
		builder.append(", useDate=");
		builder.append(useDate);
		builder.append(", startDate=");
		builder.append(startDate);
		builder.append(", endDate=");
		builder.append(endDate);
		builder.append("]");
		return builder.toString();
	}

	public boolean expireInDays(int days) {
		Calendar targetDateCalendar = Calendar.getInstance();
		targetDateCalendar.add(Calendar.DAY_OF_MONTH, days);
		Date targetDate = targetDateCalendar.getTime();

		if (getEndDate().after(new Date()) && getEndDate().before(targetDate)) {
			return true;
		}

		return false;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

}
